<template>
  <div class="detail-page">
    <van-nav-bar
      title="面经详情"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />

    <div class="content">
      <div class="head">
        <p class="title">{{ item.stem }}</p>
        <span>{{ item.createdAt }} | {{ item.views }} 浏览量 | {{ item.likeCount }} 点赞量</span>
        <div class="con">
          <img :src="item.avatar" alt="">
          <span class="other">{{ item.creator }}</span>
        </div>
      </div>
      <div class="body" v-html="item.content">
      </div>
    </div>
    <div class="opt">
      <van-icon :name=" item.likeFlag === 0 ? 'like-o' : 'like'" @click="hLike" class="like" size="25"/>
      <van-icon :name=" item.collectFlag === 0 ? 'star-o' : 'star'" @click="hCollect" class="collect" size="25" />
    </div>
  </div>
</template>

<script>
import { getArticleDetail } from '@/api/detail'
import { updataLike, updataCollect } from '@/api/article'

export default {
  name: 'DetailPage',
  data () {
    return {
      item: {}
    }
  },
  created () {
    this.getDetail()
  },
  methods: {
    onClickLeft () {
      this.$router.back()
    },
    async getDetail () {
      const res = await getArticleDetail(this.$route.params.id)
      console.log(res.data)
      this.item = res.data
    },
    async hLike () {
      await updataLike(this.item.id)
      this.$toast.success(this.item.likeFlag ? '取消成功' : '点赞成功')
      this.getDetail()
      console.log(this.item.likeFlag)
    },
    async hCollect () {
      await updataCollect(this.item.id)
      this.$toast.success(this.item.collectFlag ? '取消成功' : '收藏成功')
      this.getDetail()
      console.log(this.item.collectFlag)
    }
  }
}
</script>

<style lang="less" scoped>
.content{
  padding: 10px;
  .head{
    .title{
      font-size: 28px;
      font-weight: bold;
      margin: 0;
      line-height: 1.5;
      text-align: center;
    }
    span{
      font-size: 12px;
      color: #999;
    }
    .con{
      flex: 1;
      overflow: hidden;
      padding-left: 10px;
      display: flex;
      align-items: center;
      margin-top: 10px;
      img{
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }
      span{
        font-size: 12px;
        color: #999;
        margin: 0;
      }
    }
  }
  .body{
    margin-top: 10px;
    font-size: 14px;
    line-height: 1.5;
  }
}
.opt{
    display: flex;
    position: fixed;
    bottom: 120px;
    right: 10px;
    i{
      background: #fff;
      width: 50px;
      height: 50px;
      text-align: center;
      line-height: 50px;
      border-radius: 50%;
      margin: 0 5px;
      box-shadow: 0 0 10px #999;
      &.like{
        color: #f00;
      }
      &.collect{
        color: rgb(252, 193, 0);
      }
    }
  }
</style>
